package com.service.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.dao.ProductmDao;
import com.entity.PageBean;
import com.entity.Product;
import com.entity.ProductBigCategory;
import com.service.ProductmService;
import com.sqlSessionFactory.SqlSessionFactoryUtil;

public class ProductmServiceImpl implements ProductmService {
	//查询商品并分页
	@Override
	public List<Product> queryAllProduct(int code) throws SQLException{
		
		SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
		ProductmDao productmDao = sqlSession.getMapper(ProductmDao.class);
		PageBean<Product> pb=new PageBean();
		Map m=new HashMap();
		m.put("start", (code-1)*pb.getPageSize());
		m.put("pageSize",pb.getPageSize());
		List<Product> lists = productmDao.queryAllProduct(m);
		sqlSession.close();
		if (lists!=null) {
			return lists;
		}
			return null;
			
	}
	//查询总记录数
	@Override
	public int queryCount() throws SQLException {
		SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
		ProductmDao productmDao = sqlSession.getMapper(ProductmDao.class);
		int res= productmDao.queryCount();
		sqlSession.close();
		return res;
	}
	//删除商品
	@Override
	public int delproById(String pid) throws SQLException {
		SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
		ProductmDao productmDao = sqlSession.getMapper(ProductmDao.class);
		int res= productmDao.delproById(pid);
		sqlSession.commit();
		sqlSession.close();
		return res;
	}
	@Override
	public int addProduct(Product p) throws SQLException {
		SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
		ProductmDao productmDao = sqlSession.getMapper(ProductmDao.class);
		int res = productmDao.addProduct(p);
		sqlSession.commit();
		sqlSession.close();
		return res;
	}
	@Override
	public int updatePro(Product p) throws SQLException {
		SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
		ProductmDao productmDao = sqlSession.getMapper(ProductmDao.class);
		int res = productmDao.updatePro(p);
		sqlSession.commit();
		sqlSession.close();
		return res;
	}
	@Override
	public Product findProById(String pid) throws SQLException {
		SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
		ProductmDao productmDao = sqlSession.getMapper(ProductmDao.class);
		 Product pro = productmDao.findProById(pid);
		 sqlSession.close();
		 return pro;
		
	}
	@Override
	public List<Product> findproBycategory(String category,int code) throws SQLException{
		SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
		ProductmDao productmDao = sqlSession.getMapper(ProductmDao.class);
		PageBean pb=new PageBean();
		Map m=new HashMap();
		m.put("start", (code-1)*pb.getPageSize());
		m.put("pageSize",pb.getPageSize());
		m.put("category", category);
		List<Product> pro = productmDao.findproBycategory(m);
		sqlSession.close();
		if (pro!=null) {
			return pro;
		}
		return null;
		
	}
	
	@Override
	public int queryCountByCategory(String category) throws SQLException {
		SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
		ProductmDao productmDao = sqlSession.getMapper(ProductmDao.class);
		int records = productmDao.queryCountByCategory(category);
		sqlSession.close();
		return records;
	}
	@Override
	public List<ProductBigCategory> queryAllCategory() throws SQLException {
		SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
		ProductmDao productmDao = sqlSession.getMapper(ProductmDao.class);
		List<ProductBigCategory> bigCategory = productmDao.getBigCategory();
		sqlSession.close();
		return bigCategory;
	}


}
